package com.szh.cloud.api.user.control;


import com.alibaba.fastjson.JSON;
import com.szh.cloud.api.user.entity.ApiSearchUserEntity;
import com.szh.cloud.api.user.service.ApiUserService;
import com.szh.cloud.mybatis.eltree.ElTreeResult;
import com.szh.cloud.mybatis.entity.PtUserEntity;
import com.szh.cloud.mybatis.flexigrid.PagingBean;
import com.szh.cloud.spring.control.BaseController;
import com.szh.cloud.user.entity.UserEntity;
import com.szh.cloud.user.execption.RepeatEmailException;
import com.szh.cloud.user.execption.RepeatMobileException;
import com.szh.cloud.user.execption.RepeatUserNameException;
import com.szh.cloud.user.execption.UseNameCcupdException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class ApiUserControl extends BaseController {
    @Resource
    private ApiUserService apiUserService;
    @RequestMapping("/queryUserListByPage")
    public String queryUserListByPage(ApiSearchUserEntity entity, PagingBean paging){
        List<PtUserEntity> list = apiUserService.queryUserListByPage(entity,paging);
        return JSON.toJSONString(list);
    }

    @RequestMapping("/queryUserListTotal")
    public String queryUserListTotal(ApiSearchUserEntity entity){
        Integer total = apiUserService.queryUserListTotal(entity);
        return successCodeJSON("user.query.total",total);
    }

    @RequestMapping("/addUser")
    public String addUser(UserEntity entity){
        try {
            apiUserService.addUser(entity);
        } catch (RepeatUserNameException e) {
            return failJSON("user.user.name.repeat");
        } catch (RepeatEmailException e) {
//            e.printStackTrace();
            return failJSON("user.email.repeat");
        } catch (RepeatMobileException e) {
//            e.printStackTrace();
            return failJSON("user.mobile.repeat");
        }
        return successJSON("user.add.user.success");
    }

    @RequestMapping("/editUser")
    public String editUser(UserEntity userEntity){
        try {
            apiUserService.editUser(userEntity);
        } catch (RepeatUserNameException e) {
            return failJSON("user.user.name.repeat");
        } catch (RepeatEmailException e) {
//            e.printStackTrace();
            return failJSON("user.email.repeat");
        } catch (RepeatMobileException e) {
//            e.printStackTrace();
            return failJSON("user.mobile.repeat");
        }

        return successJSON("user.modify.success");
    }

    @RequestMapping("/deleteUserRecycleById")
    public String deleteUserRecycleById(@RequestParam("userUuids") String userUuids){
        try {
            apiUserService.deleteUserRecycleById(userUuids);
        } catch (UseNameCcupdException e) {
            e.printStackTrace();
            return failJSON("user.name.occupied");
        }
        return successJSON("user.delete.success");


    }
    @RequestMapping("/queryRoleTreeByUser")
    public ElTreeResult queryRoleTreeByUser(@RequestParam("userUuid") String userUuid){
        ElTreeResult elTreeResult = apiUserService.queryRoleTreeByUser(userUuid);
        return elTreeResult;
    }

    @RequestMapping("/saveRoleByUser")
    public String saveRoleByUser(@RequestParam("userUuids") String userUuids,@RequestParam("roleIds") String roleIds){

        apiUserService.saveRoleByUser(userUuids,roleIds);
        return successJSON("user.allocat.role.success");
    }




}